var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hasData: false,
    middle_height: "100vh",
    month: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
    month_text: "本月",
    delivery_data: [],
    detailShow: '',
    dd_style: "",
    path_data: [],
    warehouse: '全部仓库',
    warehouseId: '',
    tab: 1,
    coAdmin: app.globalData.coAdmin,
    pageSize: 10,
    pageNumber: 0,
    count: 0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - (that.data.is_schedule ? 100 : 60)
        });
      }
    });
    if (e.warehouseid) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseid
      });
    }
    if (e.filter) {
      that.setData({
        filter: true
      })
      if (e.filter == 'carrier') {
        that.setData({
          carrier_coid: e.carrier_coid
        })
      }
    }
    that.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    util.getData("/atapi/delivery/delivery/deliveryList", {
      CO_ID: app.globalData.CO_ID,
      month: that.data.month,
      warehouseId: that.data.warehouseId,
      page: that.data.page,
      pageSize: that.data.pageSize,
      carrier_coid: that.data.carrier_coid
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        var count = res.data.total;
        var pageNumber = Math.ceil(count / that.data.pageSize);
        that.setData({
          dataList: res.data.data,
          count: count,
          pageNumber: pageNumber
        });
        that.setPageStatus();
      } else {
        that.setData({
          page: 0,
          dataList: []
        });
      }
    });

  },
  changeDeliveryDate: function (e) {
    this.setData({
      month: e.detail.value,
      month_text: util.dateObjectformat(new Date(e.detail.value), 'yyyy-MM')
    });
    this.loadData();
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  showDeliveryDetail: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/order/order?id=' + id
    })
  },
  //翻页按钮状态
  setPageStatus: function () {
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function (e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  }
})
